﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using BarCode.Common;
using BarCode.Bean;

namespace LackCodePrint
{
    public class IniFileHelper
    {
        private static readonly log4net.ILog LogUtil = log4net.LogManager.GetLogger(typeof(IniFileHelper));

        public static string path;             //INI文件名  

        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key,
                    string val, string filePath);

        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def,
                    StringBuilder retVal, int size, string filePath);

        //声明读写INI文件的API函数  
        public static void IniFile()
        {
            path = AppDomain.CurrentDomain.BaseDirectory + Const.INI_FILE_NAME;
        }

        //类的构造函数，传递INI文件名  
        public static void WriteValue(string Section, string Key, string Value)
        {
            WritePrivateProfileString(Section, Key, Value, path);
        }

        //写INI文件  
        public static string ReadValue(string Section, string Key)
        {
            StringBuilder temp = new StringBuilder(255);
            int i = GetPrivateProfileString(Section, Key, "", temp, 255, path);
            return temp.ToString();
        }

        // 读取配置文件值  
        public static bool getBoolValue(string Section, string Key)
        {
            StringBuilder temp = new StringBuilder(255);
            int i = GetPrivateProfileString(Section, Key, "", temp, 255, path);
            if ("1".Equals(temp.ToString()))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public static string getStringValue(string Section, string Key)
        {
            StringBuilder temp = new StringBuilder(255);
            int i = GetPrivateProfileString(Section, Key, "", temp, 255, path);
            return temp.ToString();
        }

        /// <summary>
        /// 获取打印机名称
        /// </summary>
        /// <returns></returns>
        public static void readPrinterName()
        {
            GlobalVal.PrinterName = ReadValue("System", "PrinterName");
        }

        /// <summary>
        /// 读取Mysql数据库访问信息
        /// </summary>
        /// <returns></returns>
        public static MySqlAccessInfo ReadMySqlAccessInfo(String section)
        {

            try
            {
                MySqlAccessInfo info = new MySqlAccessInfo();
                info.SERVER_IP = ReadValue(section, "SERVER_IP");
                info.USERID = DESUtil.Decrypt(ReadValue(section, "USER_ID"));
                info.USERPWD = DESUtil.Decrypt(ReadValue(section, "USER_PWD"));
                return info;
            }
            catch (Exception ex)
            {
                LogUtil.Error(ex);
                return null;
            }

        }

    }
}
